#include <iostream>
#include <string>
#include <unistd.h>
// #include <pthread.h>
#include <thread> // C++11的线程库

using namespace std;

void* callback1(void* args)
{
    string name = (char*)args;
    while (true)
    {
        cout << name << ": " << ::getpid() << endl;
        sleep(1);
    }
}

void* callback2(void* args)
{
    string name = (char*)args;
    while (true)
    {
        cout << name << ": " << ::getpid() << endl;
        sleep(1);
    }
}

int main()
{
    // std::thread t([](){
    //     while(true)
    //     {
    //         cout << "线程运行起来啦" << endl;
    //         sleep(1);
    //     }
    // });

    // 等待就可以了
    // t.join();

    pthread_t tid1;
    pthread_t tid2;

    pthread_create(&tid1, nullptr, callback1, (void*)"thread 1");
    pthread_create(&tid2, nullptr, callback2, (void*)"thread 2");

    while (true)
    {
        cout << "我是主线程...: " << ::getpid() << endl;
        sleep(1);
    }

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);

    return 0;
}
